<template>
  <div class="app-container">
    <el-page-heade>公司钱包流水记录</el-page-heade>
    <el-divider class="table-divider"></el-divider>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="变动量(kg)" prop="dealAmount">-->
<!--        <el-input-->
<!--          v-model="queryParams.dealAmount"-->
<!--          placeholder="请输入变动量(kg)"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="业务类型" prop="dealType">
        <el-select v-model="queryParams.dealType" placeholder="请选择业务类型" clearable>
          <el-option
            v-for="dict in dict.type.c_deal_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="设备号" prop="dealFacilitNum">
        <el-input
          v-model="queryParams.dealFacilitNum"
          placeholder="请输入交易设备号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="交易时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="primary"-->
      <!--          plain-->
      <!--          icon="el-icon-plus"-->
      <!--          size="mini"-->
      <!--          @click="handleAdd"-->
      <!--          v-hasPermi="['consumer:dealCompany:add']"-->
      <!--        >新增</el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="success"-->
      <!--          plain-->
      <!--          icon="el-icon-edit"-->
      <!--          size="mini"-->
      <!--          :disabled="single"-->
      <!--          @click="handleUpdate"-->
      <!--          v-hasPermi="['consumer:dealCompany:edit']"-->
      <!--        >修改</el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="danger"-->
      <!--          plain-->
      <!--          icon="el-icon-delete"-->
      <!--          size="mini"-->
      <!--          :disabled="multiple"-->
      <!--          @click="handleDelete"-->
      <!--          v-hasPermi="['consumer:dealCompany:remove']"-->
      <!--        >删除</el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['consumer:dealCompany:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dealCompanyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="公司名称" align="center" prop="companyName"/>
      <el-table-column label="公司用户" align="center" prop="userName"/>
      <el-table-column label="联系电话" align="center" prop="userPhone"/>
      <el-table-column label="变动量(kg)" align="center" prop="dealAmount" />
      <el-table-column label="业务类型" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.c_deal_type" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="交易设备号" align="center" prop="dealFacilitNum"/>
      <el-table-column label="交易时间" align="center" prop="createTime"/>
      <!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
      <!--        <template slot-scope="scope">-->
      <!--          <el-button-->
      <!--            size="mini"-->
      <!--            type="text"-->
      <!--            icon="el-icon-edit"-->
      <!--            @click="handleUpdate(scope.row)"-->
      <!--            v-hasPermi="['consumer:dealCompany:edit']"-->
      <!--          >修改</el-button>-->
      <!--          <el-button-->
      <!--            size="mini"-->
      <!--            type="text"-->
      <!--            icon="el-icon-delete"-->
      <!--            @click="handleDelete(scope.row)"-->
      <!--            v-hasPermi="['consumer:dealCompany:remove']"-->
      <!--          >删除</el-button>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改公司钱包流水记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="公司id" prop="companyId">
          <el-input v-model="form.companyId" placeholder="请输入公司id"/>
        </el-form-item>
        <el-form-item label="来源用户id" prop="targetUserId">
          <el-input v-model="form.targetUserId" placeholder="请输入来源用户id"/>
        </el-form-item>
        <el-form-item label="变动量(kg)" prop="dealAmount">
          <el-input v-model="form.dealAmount" placeholder="请输入变动量(kg)"/>
        </el-form-item>
        <el-form-item label="业务类型" prop="dealType">
          <el-select v-model="form.dealType" placeholder="请选择业务类型">
            <el-option
              v-for="dict in dict.type.c_deal_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="交易设备号" prop="dealFacilitNum">
          <el-input v-model="form.dealFacilitNum" placeholder="请输入交易设备号"/>
        </el-form-item>
        <el-form-item label="钱包变更前数据" prop="dealOriginalJson">
          <el-input v-model="form.dealOriginalJson" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="钱包变更后数据" prop="dealDisposeJson">
          <el-input v-model="form.dealDisposeJson" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="是否删除" prop="isDelete">
          <el-input v-model="form.isDelete" placeholder="请输入是否删除"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listDealCompany,
  getDealCompany,
  delDealCompany,
  addDealCompany,
  updateDealCompany
} from "@/api/consumer/dealCompany";
import {lToMl, mlToL} from "../../../utils/unitUtil";

export default {
  name: "DealCompany",
  props: {
    exampleProp: Number,
  },
  watch: {
    exampleProp(newValue) {
      // 调用子组件的 getList 方法
      console.log("exampleProp", newValue);
      this.getList();
    },
  },
  dicts: ['c_deal_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 公司钱包流水记录表格数据
      dealCompanyList: [],
      // 日期范围
      dateRange: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyId: null,
        targetUserId: null,
        dealAmount: null,
        dealType: null,
        dealFacilitNum: null,
        dealOriginalJson: null,
        dealDisposeJson: null,
        isDelete: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        companyId: [
          {required: true, message: "公司id不能为空", trigger: "blur"}
        ],
        dealAmount: [
          {required: true, message: "变动量(kg)不能为空", trigger: "blur"}
        ],
        isDelete: [
          {required: true, message: "是否删除不能为空", trigger: "blur"}
        ],
        status: [
          {required: true, message: "状态，0：正常，1：禁用不能为空", trigger: "change"}
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    mlToL,
    /** 查询公司钱包流水记录列表 */
    getList() {
      this.loading = true;
      this.queryParams.companyId = this.exampleProp;
      console.log("this.queryParams", this.queryParams, this.dateRange);
      // this.queryParams.dealAmount = lToMl(this.queryParams.dealAmount)
      listDealCompany(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        console.log("listDealCompany", response);
        this.dealCompanyList = response.rows;
        this.dealCompanyList.forEach(item => {
          item.dealAmount = mlToL(item.dealAmount)
        })
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        dealRecordId: null,
        companyId: null,
        targetUserId: null,
        dealAmount: null,
        dealType: null,
        dealFacilitNum: null,
        dealOriginalJson: null,
        dealDisposeJson: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        isDelete: null,
        status: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.dealRecordId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加公司钱包流水记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const dealRecordId = row.dealRecordId || this.ids
      getDealCompany(dealRecordId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改公司钱包流水记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.dealRecordId != null) {
            updateDealCompany(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDealCompany(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const dealRecordIds = row.dealRecordId || this.ids;
      this.$modal.confirm('是否确认删除公司钱包流水记录编号为"' + dealRecordIds + '"的数据项？').then(function () {
        return delDealCompany(dealRecordIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('consumer/dealCompany/export', {
        ...this.queryParams
      }, `dealCompany_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
